<?php

namespace Biz\model\springHomeDecActivity;

use Biz\pockets\modelTrait\BizModelTrait;
use Doraemon\pockets\datebase\BaseDbModel;

class LuckyCharmTicket extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_brand_activity';

    const _DATABASE_ = 'brand_activity';

    const _TABLE_NAME = 'spring_home_dec_user_ticket';

    const EXCHANGE_BRICK_COUNT = 50; //锦鲤券兑换需要的砖块数

    const WINNING_YES = 1; //中奖
    const WINNING_NO = 0; //未中奖

    const TYPE_EXCHANGE = 1; //兑换
    const TYPE_GIVE = 2; //赠送

    //删除字段名称
    protected static $deleteField = 'data_status';


    use BizModelTrait;

    /**
     * 获取用户锦鲤券
     * @param $uid
     * @return array
     */
    public static function getUserTickets($uid)
    {
        $where['uid'] = $uid;
        $where['data_status'] = self::STATUS_NORMAL;
        $tickets = [];
        $result = self::dataSelect($where);
        if ($result && is_array($result)) {
            $tickets = $result;
        }
        return $tickets;
    }

    /**
     * 分页获取用户锦鲤券
     * @param $uid
     * @param $page
     * @param $offset
     * @return array
     */
    public static function pageGetUserTickets($uid, $page = 1, $offset = 20)
    {
        $where['uid'] = $uid;
        $where['data_status'] = self::STATUS_NORMAL;
        $tickets = [];
        $result = self::dataPaginate($where, [], $page, $offset);
        if ($result && is_array($result)) {
            $tickets = $result;
        }
        return $tickets;
    }

    /**
     * 用户锦鲤券数量
     * @param $uid
     * @return int
     */
    public static function getUserTicketCount($uid)
    {
        $count = 0;
        $where['uid'] = $uid;
        $where['data_status'] = self::STATUS_NORMAL;
        $count = self::dataCount($where);
        return $count;
    }

    /**
     * 兑换锦鲤券
     * @param $data
     * @return false|int
     */
    public static function exchangeTickets($data)
    {
        return self::dataMultiInsert($data);
    }

    /**
     * 获取锦鲤中奖券
     */
    public static function getWinningTicket()
    {
        $where['winning'] = self::WINNING_YES;
        $where['data_status'] = self::STATUS_NORMAL;
        $ticket = self::dataSelectInfo($where);
        return $ticket;
    }


    /**
     * @param $ticketId
     * @return bool|int
     */
    public static function setWinningTicket($ticketId){
        $where['id'] = $ticketId;
        $where['winning'] = self::WINNING_NO;
        $data['winning'] = self::WINNING_YES;
        $data['update_time'] = date('Y-m-d H:i:s',time());
        return self::dataUpdate($data,$where,true);
    }


}